<?php

/**
 * @author HuyHoang
 * @copyright 2011
 */

class LogonController extends AppController {
    var $name = 'Logon';
    var $helpers = array('Html','Ajax','Javascript','Form');
        
    function index() {
        $this->layout = "logon";
    }
    
    function check() {
        $username = $_REQUEST['username'];
        $pass = $_REQUEST['password'];
        $count = $this->Logon->find('count', array('conditions' => array('Logon.username' => $username, 'Logon.password' => $pass)));
        if ($count > 0) {
            $admin = $this->Logon->find('first', array('conditions' => array('Logon.username' => $username, 'Logon.password' => $pass)));
            $_SESSION['admin'] = $username;
            $_SESSION['admin_id'] = $admin['Logon']['id'];
            unset($_SESSION['logon_error']);
            $this->redirect(array("controller" => "customers", "action" => "index"));
        }
        else {
            unset($_SESSION['admin']);
            $_SESSION['logon_error'] = "Sai tên đăng nhập hoặc mật khẩu";
        }
        $this->redirect(array("action" => "index"));
    }
    
    function logout() {
        session_destroy();
        $this->redirect(array('action' => 'index'));
    }
}

?>
